{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "be37436f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "853d4d3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建人脸库\n",
    "\n",
    "face_library = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'display_name':'API_sourse'   \n",
    "}\n",
    "\n",
    "r = requests.post(face_library, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4782d440",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e5cb5a33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'da3109b1aabbc181c93263aa33a8c97c',\n",
       " 'time_used': 180,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647947104,21e1edb6-08bb-4fd4-9013-fb48a2514e05',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3f19c4ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取人脸库列表和信息\n",
    "\n",
    "library_info = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,  \n",
    "}\n",
    "\n",
    "r = requests.post(library_info, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1d205ed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5da3c873",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 49,\n",
       " 'facesets': [{'faceset_token': 'da3109b1aabbc181c93263aa33a8c97c',\n",
       "   'outer_id': '',\n",
       "   'display_name': 'API_sourse',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647947910,3d90334d-3fef-4eff-8577-bb61d3cd3e0f'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "26385976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'da3109b1aabbc181c93263aa33a8c97c',\n",
       " 'outer_id': '',\n",
       " 'display_name': 'API_sourse',\n",
       " 'tags': ''}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['facesets'][-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a3b3205",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e227eb3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检测人脸获取fece_token\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://i0.wp.com/starmometer.com/wp-content/uploads/2020/08/Dominic-Fike-photo2-scaled.jpg?fit=2560%2C2067&ssl=1'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5d7f631b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fb69f568",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647948118,ead586db-f268-446a-b0c0-e83dc91172a6',\n",
       " 'time_used': 1913,\n",
       " 'faces': [{'face_token': 'b850ca0a7e3f476ad177c2bd64aa74c6',\n",
       "   'face_rectangle': {'top': 548, 'left': 903, 'width': 730, 'height': 730}}],\n",
       " 'image_id': 'ipov/BN/LptLXkDgw2MvdA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "668a91f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加人脸信息到人脸库\n",
    "\n",
    "add_face = ' https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':'b850ca0a7e3f476ad177c2bd64aa74c6',\n",
    "    'faceset_token':'da3109b1aabbc181c93263aa33a8c97c'\n",
    "}\n",
    "\n",
    "r = requests.post(add_face, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d31dac9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9d4cc630",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'da3109b1aabbc181c93263aa33a8c97c',\n",
       " 'time_used': 769,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647948415,0722b91c-7c62-4474-9b12-1e51d0226ff4',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf03e84c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
